<?php

namespace App\Http\Controllers\Admin\UserManager;

use App\Enums\RedisKeyEnum;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class CommonWordsController extends BaseController
{
    /**
     * @api {get} /user-manager/common-words/list 常用语列表
     *
     * @apiGroup 用户管理-常用语管理
     *
     * @apiParam {int} page 当前页数 可选
     * @apiParam {int} page_size 分页容量 可选
     *
     * @apiSuccessExample 成功响应
     * {
    "code": 200,
    "data": {
    "current_page": 1,
    "data": [
    {
    "id": 2, 序号
    "content": "are you sure", 内容
    "type": 1, 类型：1：主播；2：用户
    "sort": 11, 排序号
    "is_default": 0, 是否默认：1：是；0：否
    }
    ],
    "first_page_url": "http://local.qipaoadmin/user-manager/common-words/list?page=1",
    "from": 1,
    "last_page": 1,
    "last_page_url": "http://local.qipaoadmin/user-manager/common-words/list?page=1",
    "next_page_url": null,
    "path": "http://local.qipaoadmin/user-manager/common-words/list",
    "per_page": 10,
    "prev_page_url": null,
    "to": 1,
    "total": 1
    }
    }
     */
    public function list(Request $request)
    {
        $v= app('validator')->make($request->all(), [
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $pageSize = $request->page_size ?: 10;

        $list = app('model_user_common_words')
            ->select('id', 'content', 'type', 'sort', 'is_default')
            ->orderBy('sort', 'desc')->paginate($pageSize);
        return Response::sendData($list);
    }

    /**
     * @api {post} /user-manager/common-words/add 添加常用语
     *
     * @apiGroup 用户管理-常用语管理
     *
     * @apiParam {string} content 内容，必须
     * @apiParam {int} type 类型：1：主播；2：用户，必须
     * @apiParam {int} sort 排序号
     * @apiParam {int} is_default 是否默认：1：是；0：否，必须
     *
     * @apiSuccessExample 成功响应
     * {"code":200,"data":[]}
     */
    public function add(Request $request)
    {
        $param = $request->only('content', 'type', 'sort', 'is_default');
        $v= app('validator')->make($param, [
            'content' => 'required|string|max:12',
            'type' => 'required|integer|in:1,2',
            'sort' => 'integer|min:0',
            'is_default' => 'required|integer|in:0,1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        return app('repo_user_common_words')->addOrEdit($param);
    }

    /**
     * @api {put} /user-manager/common-words/edit 编辑常用语
     *
     * @apiGroup 用户管理-常用语管理
     *
     * @apiParam {int} id 序号/常用语id，必须
     * @apiParam {string} content 内容，必须
     * @apiParam {int} type 类型：1：主播；2：用户，必须
     * @apiParam {int} sort 排序号
     * @apiParam {int} is_default 是否默认：1：是；0：否，必须
     *
     * @apiSuccessExample 成功响应
     * {"code":200,"data":[]}
     */
    public function edit(Request $request)
    {
        $param = $request->only('id', 'content', 'type', 'sort', 'is_default');
        $v= app('validator')->make($param, [
            'id' => 'required|integer|min:1',
            'content' => 'required|string|max:12',
            'type' => 'required|integer|in:1,2',
            'sort' => 'integer|min:0',
            'is_default' => 'required|integer|in:0,1',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        return app('repo_user_common_words')->addOrEdit($param);
    }

    /**
     * @api {delete} /user-manager/common-words/del 删除常用语
     *
     * @apiGroup 用户管理-常用语管理
     *
     * @apiParam {string} ids 常用语id，多个用英文逗号连接
     *
     * @apiSuccessExample 成功响应
     * {
     * "code":200,
     * "data":[]
     * }
     */
    public function del(Request $request)
    {
        $ids = explode(',', $request->ids);

        DB::beginTransaction();
        try {
            //删除常用语
            foreach ($ids as $id) {
                $wordsType = app('model_user_common_words')->where('id', $id)->value('type');
                if ($wordsType) {
                    $res = app('model_user_common_words')->where('id', $id)->delete();
                    if ($res) {
                        //删除缓存
                        app('redis')->hdel(sprintf(RedisKeyEnum::SYSTEM_COMMON_WORDS, $wordsType), $id);
                    }
                }
            }

            DB::commit();
            return Response::sendData([]);
        } catch (\Exception $e) {
            DB::rollBack();
            throw new BasicException(0, $e->getMessage());
        }
    }

}
